package com.gobuy.search.mqlistener;

import com.gobuy.canal.config.RabbitMQConfig;
import com.gobuy.search.service.ESManagerService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Gaozexi on 2020/6/8 22:37
 * Step by Step  and Stand on,  You Are The Best Investment!
 */

/**
 * 商品添加索引库消息监听
 */
@Component
public class SkuListener {
    @Autowired
    private ESManagerService esManagerService;

    //接收queue= IS_MARKETABLE_QUEUE 的 消息
    @RabbitListener(queues = RabbitMQConfig.IS_MARKETABLE_QUEUE)
    public void searchAdd(String spuId) {
        //id:up   id:down
        String is_marketable = spuId.split(":")[1];
        spuId = spuId.split(":")[0];
        //商品上架
        if("UP".equalsIgnoreCase(is_marketable)){
            System.out.println("上架的商品SpuId:" + spuId);
            try {
                esManagerService.importDataToESBySpuId(spuId);
            } catch (Exception e) {
                System.out.println("导入上架商品到索引库失败");
                e.printStackTrace();
            }
            System.out.println("完成商品上架,商品Id:" + spuId);
        }
        if("DOWN".equalsIgnoreCase(is_marketable)){
            System.out.println("下架的商品SpuId:" + spuId);
            try {
                esManagerService.deleteDataFromESBySpuId(spuId);
            } catch (Exception e) {
                System.out.println("从索引库删除下架商品失败");
                e.printStackTrace();
            }
            System.out.println("完成商品下架,商品Id:" + spuId);
        }
    }
}
